package com.lanqiao.companymanage.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lanqiao.companymanage.entity.Dep;
import com.lanqiao.companymanage.service.StaffMessService;
import com.lanqiao.companymanage.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * @author 何小棍
 * 部门信息管理与添加
 */
@RestController
@RequestMapping("/staffMess")
@Api(tags = "部门信息管理")
public class StaffMessController {
    @Autowired
    StaffMessService staffMessService;

    /**
     * 分页查询
     */
    @GetMapping("/depPage")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页数,默认第一页"),
            @ApiImplicitParam(name = "size", value = "每页显示条数，默认 10")})
    @ApiOperation(value = "分页查询")
    public Result depPage(Page<Dep> depPage){
        Page<Dep> page =  staffMessService.listPage(depPage);
        return Result.success(page);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/selectById")
    @ApiOperation(value = "根据id查询")
    public Result selectById(Integer id){
        Dep dep = staffMessService.selectById(id);
        if (dep == null) {
            return Result.err("找不到对应id的值");
        }else {
            return Result.success("查询成功",dep);
        }
    }

    /**
     * 部门信息添加
     * 名称、人数、工资系数
     * @param dep
     * @return
     */
    @PostMapping("/addDep")
    @ApiOperation(value = "部门信息添加")
    public Result addDep(Dep dep){
        LambdaQueryWrapper<Dep> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dep::getDepId,dep.getDepId());
        long count = staffMessService.count(wrapper);
        if (count>0){
            return Result.err("添加失败,depId:"+dep.getDepId()+" 已经存在");
        }
        int i = staffMessService.addDep(dep);
        if (i==0) {
            return Result.err("添加失败");
        }else {
            return Result.success("添加成功",null);
        }
    }

    /**
     * 根据id删除
     */
    @GetMapping("/deletedById")
    @ApiOperation(value = "根据id删除")
    public Result deletedById(Integer id)  {
        int i = staffMessService.deleteById(id);
        if (i==0) {
            return Result.err("删除失败");
        }else {
            return Result.success("删除成功",null);
        }
    }

    /**
     * 根据id修改
     */
    @PostMapping("/updateMess")
    @ApiOperation(value = "根据id修改")
    public Result updateMess(Dep dep)  {
        int i = staffMessService.updateMess(dep);
        if (i==0) {
            return Result.err("修改失败");
        }else {
            return Result.success("修改成功",null);
        }
    }

    /**
     * 查询全部部门信息
     */
    @GetMapping("/selectAll")
    @ApiOperation(value = "查询全部部门信息")
    public Result selectAll()  {
        List<Dep> depList = staffMessService.selectAll();
        return Result.success("查询成功",depList);
    }
}
